package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface SetmealMapper {

    /**
     * 根据分类id查询套餐的数量
     *
     * @param id
     * @return
     */
    @Select("select count(id) from setmeal where category_id = #{categoryId}")
    Integer countByCategoryId(Long id);

    /**
     * 根据菜品的id查询含有该菜品的套餐数量
     *
     * @param ids
     * @return
     */
    Long getByIdWithSetmealDish(List<Long> ids);

    /**
     * 根据id删除所有的口味数据
     *
     * @param ids
     */
    void deleteByIdWithFlavor(List<Long> ids);

    /**
     * 根据id删除所有的菜品数据
     *
     * @param ids
     */
    void deleteByIdWithdish(List<Long> ids);

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    Page<Setmeal> page(SetmealPageQueryDTO setmealPageQueryDTO);

    /**
     * 保存套餐基本信息
     *
     * @param setmeal
     */
    //公共字段填充
    @AutoFill(value = OperationType.INSERT)
    Long saveBaseWithSetmeal(Setmeal setmeal);

    /**
     * 保存套餐的菜品数据
     *
     * @param setmealDishes
     */
    void insertBatch(List<SetmealDish> setmealDishes);

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Update("update setmeal set status=#{status} where id = #{id}; ")
    void startOrStop(Integer status, Integer id);

    /**
     * 根据id查询套餐的基本信息
     *
     * @return
     */
    @Select("select sm.category_id, c.name as categoryName, sm.description, sm.id, sm.image, sm.name, sm.price, sm.status, sm.update_time\n" +
            "from setmeal sm left join category c on sm.category_id = c.id where sm.id = #{id}")
    SetmealVO getByIdWithDishBase(Long id);

    /**
     * 根据id查询套餐的关于菜品的额外信息
     *
     * @param id
     * @return
     */
    List<SetmealDish> getByIdDishExtendtion(Long id);

    /**
     * 根据ids删除套餐数据
     */
    void deleteByIdWithSetmeal(List<Integer> ids);

    /**
     * 根据ids删除套餐的菜品数据
     *
     * @param ids
     */
    void deleteByIdWithSetmealDish(List<Integer> ids);

    @AutoFill(OperationType.UPDATE)
    void updateById(Setmeal setmeal);

    /**
     * 动态条件查询套餐
     *
     * @param setmeal
     * @return
     */
    List<Setmeal> list(Setmeal setmeal);

    /**
     * 根据套餐id查询菜品选项
     *
     * @param setmealId
     * @return
     */
    @Select("select sd.name, sd.copies, d.image, d.description " +
            "from setmeal_dish sd left join dish d on sd.dish_id = d.id " +
            "where sd.setmeal_id = #{setmealId}")
    List<DishItemVO> getDishItemBySetmealId(Long setmealId);
}
